import { computed, onUnmounted, ref } from 'vue' 
import  dayjs  from 'dayjs'
export const useCountDown = () => {
    let timer = null
    //* 要返回响应式的数据和倒计时的函数
    const time = ref(0)
    const formatTime = computed(() => {
      return  dayjs.unix(time.value).format('mm分ss秒')
    })
    const start = (currentTime) => {
        //*每隔1s就减1
        timer= setInterval(() => {
            time.value = currentTime
            currentTime--
        }, 1000)
    }
    //*组件销毁时清除定时器
    onUnmounted(() => {
        timer&&clearInterval(timer)
    })
    return {
        formatTime,
        start
    }
}